﻿using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MobileControlGuru.Src
{
    public class JobStatusListener : IJobListener, ITriggerListener
    {
        private readonly JobStatusTracker _statusTracker;

        public JobStatusListener(JobStatusTracker statusTracker)
        {
            _statusTracker = statusTracker;
        }

        public string Name => "JobStatusListener";

        public Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default)
        {
            // 可以在这里处理任务执行被否决的情况
            return Task.CompletedTask;
        }

        public Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default)
        {
            _statusTracker.SetStatus(context.JobDetail.Key.ToString(), JobStatus.Running);
            return Task.CompletedTask;
        }

        public Task JobExecutionComplete(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default)
        {
            if (jobException == null)
            {
                _statusTracker.SetStatus(context.JobDetail.Key.ToString(), JobStatus.Completed);
            }
            else
            {
                _statusTracker.SetStatus(context.JobDetail.Key.ToString(), JobStatus.Failed);
            }
            return Task.CompletedTask;
        }

        public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default)
        {
            return Task.CompletedTask;
        }

        public Task TriggerFired(ITrigger trigger, IJobExecutionContext context, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        public Task<bool> VetoJobExecution(ITrigger trigger, IJobExecutionContext context, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        public Task TriggerMisfired(ITrigger trigger, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        public Task TriggerComplete(ITrigger trigger, IJobExecutionContext context, SchedulerInstruction triggerInstructionCode, CancellationToken cancellationToken = default)
        {
            throw new NotImplementedException();
        }

        // 实现ITriggerListener的方法以跟踪触发器状态（如果需要）
    }
}
